#!/usr/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash ShengBTE-1.2.0.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
yum install -y environment-modules time patch autoconf automake libtool numactl binutils systemd-devel valgrind flex wget tar

mkdir -p ${buildpath} ${installpath}

set -e
yum install -y environment-modules time patch autoconf automake libtool numactl binutils systemd-devel valgrind flex wget tar

module load ${installpath}/spglib-1.12.2/spglib_modulefiles

#wget -t 10 -c -P $buildpath https://doc-0s-4k-docs.googleusercontent.com/docs/securesc/m95huj2u8mqonm0rd5972juvl5hqhrcd/6dltk4ca2hqs8fsm8ngkf20j9fd9sus8/1659447225000/05141709342757804759/15616903336065137928/1Ch03hnWtZui8tj5f9XG3dbwTNqSjGoG_?e=download&ax=AI9vYm6YXQUO2atNvr3XLbugRrxH6nAgiVK2AyNcVwl77wkTaijIrV7P6eR1NudGgQhh0mNb-gK41XlDMYMJF9KrcmQQbpbQTLBWXCvdEbjYty8xP3e90W8QvmkDdOg4K_pKN5y85T2tR7K_dTGebrtiJWYAyJr3Ka7EtET0w5cx3yCELZz4FT5SwNZA7btaRid53GpP96BoxkhYJASm2lNhFt4lHu3lZKfSJucuQ9mTcfnxUO6ddaaeQgGZjAwhjm77-_UVz0kSgM_6aa3lujH5uSXFuDvjQskar862XiQFvPnVsNXb5haM8wA8d0qbN4jKVPc2kj93zd1Vs46_4m3KZYPGFoNluaDUWE89BGcFDQS1v-8VYR4HI-j8oaTBr6NFVg_AB3tgCy7A-XDtCkui3uRexCo2CNGs5LdJwUsyt4zucH_USZGGrA7JIktv3natreRy8EQ6Q2dP2t_4SjUV46wPTqUV5V8NMuijzokzlgyWjHBd9do47iGVixzZolk5yW5yiQ-qlIFAM5HRl0XwjfB9Hga1C7AxlWr1v-BbexzglWRc7Abhoi4mua6onZXcYuGOMztEMqVAIpUtfa45b87ohTlG2syvDOaQPvea9keoOmpwXwZQcsPMJ4nT2VX0xU88qcplRK_R-14Qj3ebUS3DYb1FrULzbCT7V8InluBUJaaWQPnaSAr3kRjnlTsEftNwQFzBW8yBIWQt&uuid=0a779425-2308-4a04-9264-50279839dab0&authuser=0&nonce=17capdv2ut836&user=15616903336065137928&hash=hnucni6nt1kcko290557r1lk2eeomj17
cp dependency/src/ShengBTE-v1.2.0-dffd5bb.tar.bz2 $buildpath/ShengBTE-v1.2.0-dffd5bb.tar.bz2 
tar xf $buildpath/ShengBTE-v1.2.0-dffd5bb.tar.bz2 -C $buildpath
#cp dependency/src/arch.make-ShengBTE-v1.2.0-dffd5bb.patch $buildpath/ShengBTE/Src/arch.make.patch
#dos2unix $buildpath/ShengBTE/arch.make.example
#sed -i -e "/^+LDFLAGS=/s+=+= -L${installpath}/spglib-1.12.2/lib +g" \
#       -e "/+LAPACK/s+=+= ${installpath}/lapack-3.8.0/liblapack.a+g" arch.make.patch
#cp arch.make.patch $buildpath/ShengBTE/Src
#cp $buildpath/ShengBTE/arch.make.example $buildpath/ShengBTE/Src/arch.make
cd $buildpath/ShengBTE/Src
#patch -p0 < arch.make.patch

cat > arch.make << EOF
SHELL:=/bin/bash

CC=mpicc
CXX=mpicxx
FC=mpifort

FFLAGS=-O2
LDFLAGS= -L${installpath}/spglib-1.12.2/lib -lsymspg
MPIFC=mpif90
KML_ROOT=${installpath}/kml
KML_LAPACK_ROOT=\$(KML_ROOT)/lib
ADAPT_ROOT=\$(KML_ROOT)/lib
KML_BLAS_ROOT=\$(KML_ROOT)/lib/kblas/pthread

KML=-L\$(KML_BLAS_ROOT) -lkblas -lflang -lpthread -lm
LAPACK= ${installpath}/lapack-3.8.0/liblapack.a \$(KML)
LIBS=\$(LAPACK)
EOF


export CC=`which clang`
export CXX=`which clang++`
export FC=`which flang`

make
mkdir -p $installpath/ShengBTE-1.2.0 && cp -r $buildpath/ShengBTE/ShengBTE $installpath/ShengBTE-1.2.0
cat > $installpath/ShengBTE-1.2.0/ShengBTE_modulefiles << EOF
#%Module1.0
conflict ShengBTE

module use $installpath/ShengBTE-1.2.0/install
module load $installpath/bisheng-compiler-2.4.0/bisheng_modulefiles
module load $installpath/hmpi-1.2.0/hmpi_modulefiles
module load $installpath/kml/kml_modulefiles
module load $installpath/lapack-3.8.0/lapack_modulefiles
module load $installpath/spglib-1.12.2/spglib_modulefiles

variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set ShengBTE \$pwd
setenv ShengBTE \$ShengBTE
prepend-path PATH \$ShengBTE
EOF
#rm -rf ${buildpath}
